//
//  LPTAopenpretionTableViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/4.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTAopenpretionTableViewController.h"
#import "LPTAopenpretionTableViewCell.h"
#import "LPTAopenContainerEntity.h"

@interface LPTAopenpretionTableViewController ()

@property (nonatomic, assign) NSInteger page;

@end

@implementation LPTAopenpretionTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initSubViews];
    
    self.page = 1;
    
    [self requestAopenpretionList];
}

-(void)initSubViews
{
    self.tableView.rowHeight = 196;
    self.tableView.tableFooterView = [UIView new];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.page = 1;
        
        [self requestAopenpretionList];
        
    }];
    
    self.tableView.mj_header = header;
    
}

//已开处方列表
-(void)requestAopenpretionList
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/prescription_list" withBody:@{@"page":@(self.page),@"size":@(1000)} withMethod:YTKRequestMethodGET];
    [request startRequestWithClass:[LPTAopenContainerEntity class] completeSuccess:^(__kindof LPTBaseRequest * _Nullable request, LPTAopenContainerEntity *data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        if (self.page == 1) {
            [self.dataSource removeAllObjects];
        }
        
        [self.dataSource addObjectsFromArray:data.prescriptions];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        
    }];
}

#pragma mark - Table view data source
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPTAopenpretionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTAopenpretionTableViewCell" forIndexPath:indexPath];
    LPTPrescriptionsEntity *entity = self.dataSource[indexPath.row];
    [cell refreshWithPrescriptionEntity:entity];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
